<template>
  <el-dialog title="title" :visible.sync="dialogShow" @close="closeDalog">
    <!-- ⭐v-model是用来收集和回显对应勾选数据的 和label字段关联 checkList里有哪个<=>对应被勾选 -->
    <!-- ⭐默认我们显示的也是label的内容，除非我们使用插值表达式覆盖label内容-->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <br />
    <el-row>
      <el-button @click="setRoleSubmit" size="small" type="primary">
        确定
      </el-button>
      <el-button @click="dialogShow = false" size="small">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { getEmployeesInfoApi, assignRoles } from '@/api/employees'
export default {
  name: 'SetRole',
  data() {
    return {
      dialogShow: false,
      roleList: [], // 所有角色
      checkList: [], // 勾选中的
      userId: null // 当前员工id
    }
  },
  methods: {
    closeDalog() {
      this.checkList = []
      this.userId = null
    },
    // 获取所有角色
    async getAllRoleList() {
      const { rows } = await getRoleListApi({
        page: 1,
        pagesize: 1000
      })
      console.log(rows)
      this.roleList = rows
    },
    // 获取当前员工信息
    async getCurrentRole(id) {
      const res = await getEmployeesInfoApi(id)
      console.log(res)
      this.userId = id
      this.checkList = res.roleIds || []
    },
    // 设置角色信息
    async setRoleSubmit() {
      await assignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('关联成功')
    }
  }
}
</script>

<style></style>
